package com.barunsw.bwf.bims.channel;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.barunsw.bwf.bims.vo.ChannelVo;
import com.barunsw.bwf.common.vo.ResultVo;
import com.barunsw.bwf.constants.Result;

@Controller
@RequestMapping("/channel")
public class ChannelController {
	private static Logger LOGGER = LoggerFactory.getLogger(ChannelController.class);
	
	@Autowired
	ChannelService channelService;
	
	@RequestMapping(value="/{deviceId}", method=RequestMethod.GET)
	public @ResponseBody List<ChannelVo> getChannelList(@RequestBody @PathVariable long deviceId) throws Exception {
		List<ChannelVo> channelList = channelService.getChannelList(deviceId);
		return channelList;
	}
	
	@RequestMapping(value="/{deviceId}/{channelId}", method=RequestMethod.GET)
	public @ResponseBody ChannelVo getChannelById(@RequestBody @PathVariable long deviceId, 
			@RequestBody @PathVariable long channelId) throws Exception {
		ChannelVo channel = channelService.getChannelById(deviceId, channelId);
		return channel;
	}
	
	@RequestMapping(value="", method=RequestMethod.POST)
	public @ResponseBody ResultVo createDevice(@RequestBody ChannelVo channelVo) throws Exception {
		LOGGER.debug("createChannel");
		
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = channelService.createChannel(channelVo);
		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("Channel 추가시 오류가 발생하였습니다.");
		}
		
		return resultVo;
	}
	
	@RequestMapping(value="", method=RequestMethod.PUT)
	public @ResponseBody ResultVo updateChannel(@RequestBody ChannelVo channelVo) throws Exception {
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = channelService.updateChannel(channelVo);
		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("Channel 수정시 오류가 발생하였습니다.");
		}
		
		return resultVo;
	}
	
	@RequestMapping(value="/{channelId}", method=RequestMethod.DELETE)
	public @ResponseBody ResultVo deleteChannel(@RequestBody @PathVariable long channelId) throws Exception {
		ResultVo resultVo = new ResultVo(Result.OK, "");
		
		int result = channelService.deleteChannel(channelId);
		if ( result <= 0 ) {
			resultVo.setResult(Result.NOK);
			resultVo.setReason("Channel 삭제시 오류가 발생하였습니다.");
		}
		
		return resultVo;
	}
}
